package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.constant.MessageCodeConstant;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.ShowCardBannerDto;
import com.aiti.lulian.entity.ShowCardBanner;
import com.aiti.lulian.feign.CloudShowManagerSubService;
import com.aiti.lulian.service.ShowCardBannerService;
import com.aiti.lulian.vo.ShowCardBannerVo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工信门户首页 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/cardBanner")
public class ShowCardBannerController extends WebController {

    @Autowired
    private ShowCardBannerService cardBannerService;

    @Autowired
    private CloudShowManagerSubService userService;

    @GetMapping("/page")
    public Message bannerList(ShowCardBannerDto dto) {
        Page page = this.startPage();
        List<String> userRoleList = userService.getUserRoleList(findUserId());
        if(CollectionUtils.isEmpty(userRoleList) || !userRoleList.contains("1")){
            Message userInfo = userService.getUserInfo(findUserId());
            if(userInfo != null && userInfo.getCode() == MessageCodeConstant.MSG_SUCCESS_CODE){
                Map obj = (Map)userInfo.getObj();
                dto.setCity(String.valueOf(obj.get("city")));
                dto.setCounty(String.valueOf(obj.get("region")));
            }else{
                throw new RuntimeException("获取用户信息有误");
            }
        }
        List<ShowCardBannerVo> list = cardBannerService.bannerList(page, dto);
        return Message.success(this.getPageResult(list, (int)page.getTotal()));
    }

    @GetMapping("/latestBanner")
    public Message latestBanner(ShowCardBannerDto dto) {
        if(StrUtil.isBlank(dto.getProvince()) && StrUtil.isBlank(dto.getCity()) && StrUtil.isBlank(dto.getCounty())){
            return Message.fail("请设置当前登录用户的所属地区");
        }
        return Message.success(cardBannerService.latestBanner(dto));
    }

    @GetMapping
    public Message detail(@RequestParam("baseId") String baseId) {
        return Message.success(cardBannerService.getById(baseId));
    }

    @DeleteMapping
    public Message delete(@RequestParam("baseId") String baseId) {
        return Message.success(cardBannerService.removeById(baseId));
    }


    @PostMapping
    public Message insert(@RequestBody ShowCardBanner entity) {
        entity.setCreator(findUserId());
        cardBannerService.save(entity);
        return Message.success();
    }

    @PutMapping
    public Message update(@RequestBody @Validated ShowCardBanner entity) {
        entity.setUpdateBy(findUserId());
        cardBannerService.updateById(entity);
        return Message.success();
    }


}
